package com.max.sportmanage.util;

import com.max.sportmanage.entity.SysUser;
import lombok.val;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import springfox.documentation.spi.service.contexts.SecurityContext;

/**
 * @program: java-sport-manage
 * @description:获取当前登录用户的基本信息
 * @author: max-qaq
 * @create: 2021-10-26 10:58
 **/
public class SecurityUtil {

    /**
     * 获取用户信息
     * @return
     */
    public static SysUser getUser(){
        SysUser user = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        user.setPassword(null);
        return user;
    }

    /**
     * 获取用户名
     * @return
     */
    public static String getUserName(){
        return getUser().getUsername();
    }

    /**
     * 获取用户id
     * @return
     */
    public static Long getUserId(){
        return getUser().getId();
    }
}
